{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I love learning python\n",
      "\n",
      "because python is fun\n",
      "\n",
      "and also easy to use\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function TextIOWrapper.close()>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# File IO\n",
    "some_sentences = '''\\\n",
    "I love learning python\n",
    "because python is fun\n",
    "and also easy to use\n",
    "'''\n",
    "\n",
    "# Open for 'w'irting\n",
    "f = open('sentences.txt', 'w')\n",
    "\n",
    "# Write text to File\n",
    "f.write(some_sentences)\n",
    "f.close()\n",
    "\n",
    "# If not specifying mode, 'r'ead mode is default\n",
    "f = open('sentences.txt')\n",
    "while True:\n",
    "    line = f.readline()\n",
    "    # Zero length means End Of File\n",
    "    if len(line) == 0:\n",
    "        break\n",
    "    print(line)\n",
    "\n",
    "# close the File\n",
    "f.close"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
